﻿#Region "Imports"
Imports System.DirectoryServices
#End Region

Public Class IIsApplicationPool
    Implements IDisposable

#Region "Privates"

    Private _ApplicationPoolEntry As DirectoryEntry

#End Region

#Region "Constructors and Destructors"

    Public Sub New(ByVal ApplicationPoolEntry As DirectoryEntry)

        If ApplicationPoolEntry.SchemaClassName = ApplicationPoolSchemaClassName Then

            _ApplicationPoolEntry = ApplicationPoolEntry
            _ApplicationPoolEntry.RefreshCache()

        Else

            ConstructorEx(ApplicationPoolSchemaClassName)

        End If

    End Sub

    Protected Overrides Sub Finalize()

        If Not _ApplicationPoolEntry Is Nothing Then _ApplicationPoolEntry.Close()

    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose

        If Not _ApplicationPoolEntry Is Nothing Then _ApplicationPoolEntry.Close()

    End Sub

#End Region

#Region "Properties"

    Public ReadOnly Property Id() As String
        Get

            Return _ApplicationPoolEntry.Name

        End Get
    End Property

    Public ReadOnly Property State() As AppPoolStates
        Get

            Return GetProperty("AppPoolState", _ApplicationPoolEntry)

        End Get
    End Property

    Public Property IdentityType() As AppPoolIdentityTypes
        Get

            Return GetProperty("AppPoolIdentityType", _ApplicationPoolEntry)

        End Get
        Set(ByVal value As AppPoolIdentityTypes)

            SetProperty("AppPoolIdentityType", value, _ApplicationPoolEntry)

        End Set
    End Property

    Public Property IdentityUserName() As String
        Get

            Return GetProperty("WAMUserName", _ApplicationPoolEntry)

        End Get
        Set(ByVal value As String)

            SetProperty("WAMUserName", value, _ApplicationPoolEntry)

        End Set
    End Property

    Public Property IdentityPassword() As String
        Get

            Return GetProperty("WAMUserPass", _ApplicationPoolEntry)

        End Get
        Set(ByVal value As String)

            SetProperty("WAMUserPass", value, _ApplicationPoolEntry)

        End Set
    End Property

    Public Property AutoStart() As Boolean
        Get

            Return GetProperty("AppPoolAutoStart", _ApplicationPoolEntry)

        End Get
        Set(ByVal value As Boolean)

            SetProperty("AppPoolAutoStart", value, _ApplicationPoolEntry)

        End Set
    End Property

    Public Property IdleTimout() As Integer
        Get

            Return GetProperty("IdleTimout", _ApplicationPoolEntry)

        End Get
        Set(ByVal value As Integer)

            SetProperty("IdleTimeout", value, _ApplicationPoolEntry)

        End Set
    End Property

#End Region

#Region "Public Functions and Methods"

    Public Sub StartPool(Optional ByVal WaitTillDone As Boolean = True)

        If WaitTillDone Then

            _ApplicationPoolEntry.Invoke("Start", Nothing)

        Else

            SetProperty("AppPoolCommand", 1, _ApplicationPoolEntry)

        End If

        _ApplicationPoolEntry.RefreshCache()

    End Sub

    Public Sub StopPool(Optional ByVal WaitTillDone As Boolean = True)

        If WaitTillDone Then

            _ApplicationPoolEntry.Invoke("Stop", Nothing)

        Else

            SetProperty("AppPoolCommand", 2, _ApplicationPoolEntry)

        End If

        _ApplicationPoolEntry.RefreshCache()

    End Sub

    Public Sub RecyclePool()

        _ApplicationPoolEntry.Invoke("Recycle", Nothing)

    End Sub

    Public Function EnumAppsInPool() As String()

        Dim oApps() As Object = _ApplicationPoolEntry.Invoke("EnumAppsInPool", Nothing)
        Dim sApps() As String = {}

        If oApps.Length > 0 Then

            ReDim sApps(oApps.Length - 1)

            For iApp = 0 To oApps.Length - 1

                sApps(iApp) = oApps(iApp).ToString

            Next

        End If

        Return sApps

    End Function

#End Region

End Class
